﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NP.Model;

namespace NP.BL
{
    public class CustomersSummary
    {
        public CustumerSummaryViewModel[] GetCustomers()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                var customers = db.Customers.ToArray()
                    .Select(n => new CustumerSummaryViewModel
                    {
                        CustomerID = n.CustomerID,
                        CunstomerName = n.CompanyName,
                        TotalPurchases =
                        n.Orders.Select(m => m.Order_Details
                            .Select(o => o.Quantity * o.UnitPrice * (decimal)(1 - o.Discount)).Sum()).Sum()
                    }).OrderByDescending(n => n.TotalPurchases).ToArray();

                foreach (var item in customers)
                {
                    item.Type = CustomersSummary.Type(item.TotalPurchases);
                }

                return customers;
            }
        }

        static string Type(decimal s)
        {
            if (s > 100000)
                return "Gold";
            else if (s > 10000)
                return "Silver";
            else
                return "Normal";
        }
    }
}
